<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Services\BrandService\BrandToolService;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\SupplierService\SupplierService;
use DB;
use Illuminate\Http\Request;

class SupplierBackController extends Controller
{
    public function __construct(SupplierService $supplier)
    {
        $this->SupplierService = $supplier;
    }

    /**
     * @return $this
     * 供货商列表
     */
    public function supplierList()
    {
        $lists = DB::table('suppliers')->where('state', 1)->orderBy('created_at', 'desc')
            ->distinct()->paginate(10);
        return view('backstage.supplier.supplier_list')->with(
            ['lists' => $lists]
        );
    }

    /**
     * @return $this
     * 添加供货商页面
     */
    public function addSupplier()
    {
        $tempTool = new ProductTempTool();
        $temp_lists = $tempTool->getAllProductTempSimpleInfo();
        return view('backstage.supplier.add_supplier')->with('temp_lists', $temp_lists);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * 添加供货商
     */
    public function postAddSupplier(Request $request)
    {
        $data = $this->SupplierService->createSupplier($request);
        if ($data['state'] != 'M00000') {
            return redirect()->back()->with('error_message', $data['message']);
        } else {
            return redirect('/backstage/supplier/list')->with('success_message', $data['message']);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * 删除
     */
    public function delete(Request $request)
    {
        $supplier_id = $request->get('supplier_id');
        DB::table('suppliers')->where('supplier_id', $supplier_id)->update([
            'state' => 0,
            'updated_at' => time(),
        ]);
        return redirect()->back()->with('success_message', '操作成功');
    }

    /**
     * @param Request $request
     * @return $this
     * 详情页面
     */
    public function detail(Request $request)
    {
        $supplier_id = $request->get('supplier_id');
        $detail = DB::table('suppliers')->where('supplier_id', $supplier_id)->first();
        $temp_ids = DB::table('suppliers')->where('supplier_id', $supplier_id)->value('template_ids');
        $temp_ids_array = explode(",", $temp_ids);
        $product_temps = DB::table('product_temps')->whereIn('product_temp_id', $temp_ids_array)
            ->where(['state' => 1])->select('product_temp_id', 'name', 'brand_id')->get()->toArray();
        foreach ($product_temps as $product_temp) {
            $product_temp->brand_name = BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        return view('backstage.supplier.supplier_detail')->with(
            ['detail' => $detail, 'product_temps' => $product_temps]);
    }

    /**
     * @param Request $request
     * @return $this
     * 编辑信息页面
     */
    public function edit(Request $request)
    {
        $tempTool = new ProductTempTool();
        $temp_lists = $tempTool->getAllProductTempSimpleInfo();
        $supplier_id = $request->get('supplier_id');
        $detail = DB::table('suppliers')->where('supplier_id', $supplier_id)->first();
        $temp_ids = DB::table('suppliers')->where('supplier_id', $supplier_id)->value('template_ids');
        $temp_ids_array = explode(",", $temp_ids);
        $product_temps = DB::table('product_temps')->whereIn('product_temp_id', $temp_ids_array)
            ->where(['state' => 1])->select('product_temp_id', 'name', 'brand_id')->get()->toArray();
        foreach ($product_temps as $product_temp) {
            $product_temp->brand_name = BrandToolService::getBrandNameByBrandId($product_temp->brand_id);
        }
        return view('backstage.supplier.edit_supplier')->with(
            ['temp_lists' => $temp_lists,
                'detail' => $detail,
                'product_temps' => $product_temps]
        );
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * 编辑信息
     */
    public function postEditSupplier(Request $request)
    {
        $data = $this->SupplierService->edit($request);
        $supplier_id = $request->supplier_id;
        if ($data['state'] != 'M00000') {
            return redirect()->back()->with('error_message', $data['message']);
        } else {
            return redirect('backstage/supplier/detail?supplier_id=' . $supplier_id)->with('success_message', $data['message']);
        }
    }

}
